PrepareLevel0:
	;; position of twoplayer scoreboard
	;; VRAM C0BA
	move.l	#$40BA0003,d7
	bsr	PrepareLevelCommon

	btst	#TwoPlayerFlag,Flags
	bne.s	PrepareLevel0TwoPlayer

PrepareLevel0OnePlayer:	
	lea	StartSpriteLevel0Initial(pc),a5
	;; sprite load follows branch
	bra.s	PrepareLevel0Final
	
PrepareLevel0TwoPlayer:	
	lea	StartSpriteLevel0TwoPlayerInitial(pc),a5
	
PrepareLevel0Final:	
	bsr	SpriteLoad

	lea	Level0TruckAnimateMove(pc),a5
	move.l	a5,VintAnimate

	bsr	Level0RefreshLetters
	bsr	FadeIn
	
Level0Loop:	
	bsr	WaitForVint

	;; demo mode check
	btst	#DemoFlag,Flags
	beq.s	Level0SkipDemoMode
	;; in demo mode, exit on button press
	btst	#StartButtonFlag,Flags
	bne	PrepareTitleFadeOut
	;; demo only shows for a certain amount of frames
	cmpi.w	#VintWaitDemo,VintWait
	bge	PrepareTitleFadeOut
	;; determine where to move demo Riley
	clr.b	d1
	cmpi.b  #$20,LetterSprite+5
	beq.s	Level0DemoGoToBin
Level0DemoGoToLetter:
	move.w	RileySprite,d0
	addq.w	#$0008,d0
	cmp.w	LetterSprite,d0
	ble.s	Level0DemoDoNotMoveUp
	bset	#bitUp,d1
Level0DemoDoNotMoveUp:	
	move.w	RileySprite+6,d0
	addq.w	#$0004,d0
	cmp.w	LetterSprite+6,d0
	bgt.s	Level0DemoMoveLeft
	blt.s	Level0DemoMoveRight
	bra.s	Level0DemoUpdatePorts
Level0DemoGoToBin:
	move.w	RileySprite,d0
	addq.w	#$0008,d0
	cmp.w	BinSprite,d0
	bge.s	Level0DemoDoNotMoveDown
	bset	#bitDown,d1
Level0DemoDoNotMoveDown:	
	move.w	RileySprite+6,d0
	subq.w	#$0004,d0
	cmp.w	BinSprite+6,d0
	bgt.s	Level0DemoMoveLeft
	blt.s	Level0DemoMoveRight
	bra.s	Level0DemoUpdatePorts
Level0DemoMoveLeft:	
	bset	#bitLeft,d1
	bra.s	Level0DemoUpdatePorts
Level0DemoMoveRight:	
	bset	#bitRight,d1
Level0DemoUpdatePorts:
	;; since game boards are symetrical, apply same input to both ports
	move.b	d1,Port1Hold
	move.b	d1,Port2Hold
Level0SkipDemoMode:	
	;; check for sprite collision
	move.w   (a4),d0
	andi.b   #$0020,d0
	beq      Level0MoveRiley1
	;; there is a sprite collision
	;; check for player 1 collision
	;; all letters are above $A0+$7F+8 ($0128)
	;; and below $80+320-24-24 ($0130)
	cmpi.w   #$012C,RileySprite
	bge.s    Level0PossibleBinCollision
	;; could be a letter collision due to riley y position
	;; see if letter already disabled (set tile to $20)
	cmpi.b   #$20,LetterSprite+5
	beq.s    Level0Player2Collision
	;; collision check
	lea	RileySprite,a5
	lea	LetterSprite,a6
	bsr	CheckSpriteCollision
	bne.s	Level0Player2Collision
	;; it is a letter collision
	;; disable letter sprite by setting it to clear tile
	;; this will indicate the letter was picked up
	move.b  #$20,LetterSprite+5
	bra.s   Level0Player2Collision
Level0PossibleBinCollision:
	;; could be a bin collision due to riley y position
	;; see if letter is $00 (means it was picked up)
	cmpi.b   #$20,LetterSprite+5
	bne.s    Level0Player2Collision
	;; collision check
	lea	RileySprite,a5
	lea	BinSprite,a6
	bsr	CheckSpriteCollision
	bne.s	Level0Player2Collision
	;; is a bin collision
	;; now update the score
	lea     EndScoreAscii,a5
        move.l  #$40900003,d0
	move.w  #(EndScoreAscii-StartScoreAscii-1),d1
	clr.w	d2
	move.b  WordIndex,d2
	;; make high priority (so score will go above other things)
	move.w  #$8000,d4
	bsr	UpdateAndDisplayScoreAndWord

	;; get next character
	clr.w	d0
	move.b  WordIndex,d0
	clr.w	d1
	move.b  WordIndex2,d1
	bsr	GetNextLetter
	move.b	d0,WordIndex

	clr.b	LetterSprite+5

	btst	#FinishedWordFlag,WordFlags
	beq.s	Level0Player2Collision
	;; add in one player truck animation when finish a word!
	move.w	#($80-56),TruckLeftSprite+6
		
Level0Player2Collision:	
	;; check for player 2 collision
	btst	#TwoPlayerFlag,Flags
	beq.s	Level0CheckRefreshLetters
	;; all letters are above $A0+$7F+8 ($0128)
	;; and below $80+320-24-24 ($0130)
	cmpi.w   #$012C,Riley2Sprite
	bge.s    Level0PossibleBin2Collision
	;; could be a letter collision due to riley y position
	;; see if letter already disabled (set tile to $20)
	cmpi.b   #$20,Letter2Sprite+5
	beq.s   Level0CheckRefreshLetters
	;; collision check
	lea	Riley2Sprite,a5
	lea	Letter2Sprite,a6
	bsr	CheckSpriteCollision
	bne.s	Level0CheckRefreshLetters
	;; is a letter collision
	;; disable letter sprite by setting it to clear tile
	;; this will indicate the letter was picked up
	move.b  #$20,Letter2Sprite+5
	bra.s   Level0CheckRefreshLetters
Level0PossibleBin2Collision:
	;; could be a bin collision due to riley y position
	;; see if letter is $00 (means it was picked up)
	cmpi.b   #$20,Letter2Sprite+5
	bne.s    Level0CheckRefreshLetters
	;; collision check
	lea	Riley2Sprite,a5
	lea	Bin2Sprite,a6
	bsr	CheckSpriteCollision
	bne.s	Level0CheckRefreshLetters
	;; is a bin collision
	;; now update the score
	lea     EndScoreAscii2,a5
	move.l	#$40C60003,d0
	move.w  #(EndScoreAscii2-StartScoreAscii2-1),d1
	clr.w	d2
	move.b  WordIndex2,d2
	;; make high priority (so score will go above other things)
	move.w  #$A000,d4
	bsr	UpdateAndDisplayScoreAndWord

	;; get next character
	clr.w	d0
	move.b  WordIndex2,d0
	clr.w	d1
	move.b  WordIndex,d1
	bsr	GetNextLetter
	move.b	d0,WordIndex2

	clr.b	Letter2Sprite+5
	
Level0CheckRefreshLetters:	
	bclr	#RefreshLettersWordFlag,WordFlags
	beq.s	Level0CheckNewLevel
	bsr	Level0RefreshLetters
	
Level0CheckNewLevel:	
	;; check if need to go to next level
	;; demo cannot go to another level
	bclr	#NewLevelFlag,WordFlags
	beq.s	Level0MoveRiley1
	btst	#DemoFlag,Flags
	bne.s	Level0MoveRiley1
	bsr	LevelTransition
	bra	PrepareLevel1
	
Level0MoveRiley1:	
	lea	RileySprite,a5
        move.b  Port1Hold,d0
	bsr.s	Level0MoveRiley
Level0MoveRiley2:	
	lea	Riley2Sprite,a5
        move.b  Port2Hold,d0
	bsr.s	Level0MoveRiley
EndLevel0Loop:	
	bra     Level0Loop


	;; subroutines for level 1

	
	;; destroys d1.l
Level0MoveRiley:
	;; check up and down
	moveq	 #$07,d1
        btst	 #0,d0
        beq.s    Level0MoveRileyCheckDown
	;; up was pressed
        cmpi.w   #$0080,(a5)
	;; if not moving check left/right
        ble.s    Level0MoveRileyCheckLeftAndRight
	;; have room to move up
        subq.w   #$0001,(a5)
	;; change to up tile
        move.b   #$86,5(a5)
	;; see if need to flip tile horizontally based on animation timer
        and.b    VintCounter+1,d1
	bne.s    Level0MoveRileyCheckLeftWithUpOrDown
	;; flip horizontal bit in sprite
	eor.b    #$08,4(a5)
	bra.s    Level0MoveRileyCheckLeftWithUpOrDown
Level0MoveRileyCheckDown:
        btst	#1,d0
        beq.s    Level0MoveRileyCheckLeftAndRight
	;; down was pressed
        cmpi.w   #$0148,(a5)
	;; if not moving check left/right
        bge.s    Level0MoveRileyCheckLeftAndRight
	;; have room to move down
        addq.w   #$0001,(a5)
	;; change to down tile
        move.b   #$80,5(a5)
	;; see if need to flip tile horizontally based on animation timer
        and.b   VintCounter+1,d1
        bne.s    Level0MoveRileyCheckLeftWithUpOrDown
	;; flip horizontal bit in sprite
        eor.b    #$08,4(a5)
Level0MoveRileyCheckLeftWithUpOrDown:
        btst	#2,d0
        beq.s    Level0MoveRileyCheckRightWithUpOrDown
	;; left was pressed (with up or down)
        cmpi.w   #$0080,6(a5)
        ble.s    Level0MoveRileyDone
	;; have room to move left
        subq.w   #$0001,6(a5)
	bra.s    Level0MoveRileyDone
Level0MoveRileyCheckRightWithUpOrDown:
        btst	#3,d0
        beq.s    Level0MoveRileyDone
	;; right was pressed (with up or down)
        cmpi.w   #$01b0,6(a5)
        bge.s    Level0MoveRileyDone
	;; have room to move left
        addq.w   #$0001,6(a5)
	bra.s    Level0MoveRileyDone
Level0MoveRileyCheckLeftAndRight:
        btst	#2,d0
        beq.s    Level0MoveRileyCheckRight
	;; left was pressed
        cmpi.w   #$0080,6(a5)
        ble.s    Level0MoveRileyDone
        subq.w   #$0001,6(a5)
	bset	 #3,4(a5)
	bra.s	 Level0MoveRileyAnimateStride
Level0MoveRileyCheckRight:
        btst	#3,d0
        beq.s   Level0MoveRileyDone
	;; right was pressed
        cmpi.w   #$01b0,6(a5)
        bge.s   Level0MoveRileyDone
        addq.w   #$0001,6(a5)
	bclr	 #3,4(a5)
Level0MoveRileyAnimateStride:
	and.b    VintCounter+1,d1
        bne.s   Level0MoveRileyDone
	bsr	NextStrideFrame
Level0MoveRileyDone:
	rts


	;; updates letters, and for level 1 the letter x and y positions
	;; no inputs
	;; destroys a5
Level0RefreshLetters:	
	bsr	RefreshLetters

	movem.w d0-d3,-(a7)
	;; update letter x location
	lea	StartWordX,a5

	clr.w	d0
	clr.w	d2
	move.b	WordIndex,d0
	move.b	WordIndex2,d2
	move.b	(a5,d0.w),d1
	move.b	(a5,d2.w),d3
	;; in two player mode, have less x space to use
	btst	#TwoPlayerFlag,Flags
	bne.s	Level0RefreshLetterPositionsTwoPlayerX

	andi.w  #$00FF,d1
	;; $80 + (320/2 - 256/2 - 8/2)
	addi.w  #$009C,d1
	move.w  d1,LetterSprite+6
	bra.s 	Level0RefreshLetterPositionsY

Level0RefreshLetterPositionsTwoPlayerX:
	andi.w  #$007F,d1
	andi.w  #$007F,d3
	;; $80 + (320/4 - 128/2 - 8/2)
	addi.w  #$008C,d1
	addi.w  #$012C,d3
	move.w  d1,LetterSprite+6
	move.w  d3,Letter2Sprite+6

Level0RefreshLetterPositionsY:	
	;; update letter y location
	lea	StartWordY,a5

	move.b	(a5,d0.w),d1
	move.b	(a5,d2.w),d3
	andi.w   #$007F,d1
	andi.w   #$007F,d3
	;; gives 4 pixels from bottom of score
	;; and gives 8 pixels above riley+bin stacked
	addi.w   #$00A0,d1
	addi.w   #$00A0,d3
	move.w  d1,LetterSprite+0
	move.w  d3,Letter2Sprite+0

	movem.w (a7)+,d0-d3
	rts


;;; data used for level 1


StartSpriteLevel0Initial:
RileySpriteLevel0Initial:
	dc.b $01,$2C,$06,$01,$C8,$80,$01,$18
TruckLeftSpriteLevel0Initial:
	dc.b $01,$40,$0F,$02,$00,$AD,$01,$E0
TruckRightSpriteLevel0Initial:
	dc.b $01,$40,$0B,$03,$00,$BD,$01,$E0
BinSpriteLevel0Initial:
	dc.b $01,$48,$09,$04,$00,$9E,$01,$14
LetterSpriteLevel0Initial:
	dc.b $00,$D0,$00,$00,$00,$00,$01,$1C
EndSpriteLevel0Initial:	


StartSpriteLevel0TwoPlayerInitial:
RileySpriteLevel0TwoPlayerInitial:
	dc.b $01,$2C,$06,$05,$C8,$80,$00,$C8
TruckLeftSpriteLevel0TwoPlayerInitial:
	dc.b $01,$40,$0F,$02,$00,$AD,$01,$E0
TruckRightSpriteLevel0TwoPlayerInitial:
	dc.b $01,$40,$0B,$06,$00,$BD,$01,$E0
BinSpriteLevel0TwoPlayerInitial:
	dc.b $01,$48,$09,$08,$00,$9E,$00,$C4
LetterSpriteLevel0TwoPlayerInitial:
	dc.b $00,$D0,$00,$09,$00,$00,$01,$1C
Riley2SpriteLevel0TwoPlayerInitial:
	dc.b $01,$2C,$06,$01,$E8,$80,$01,$68
Truck2LeftSpriteLevel0TwoPlayerInitial:
	dc.b $01,$40,$0F,$07,$28,$AD,$01,$E0
Truck2RightSpriteLevel0TwoPlayerInitial:
	dc.b $01,$40,$0B,$03,$28,$BD,$01,$E0
Bin2SpriteLevel0TwoPlayerInitial:
	dc.b $01,$48,$09,$04,$28,$9E,$01,$64
Letter2SpriteLevel0TwoPlayerInitial:
	dc.b $00,$D0,$00,$00,$20,$00,$01,$6C
EndSpriteLevel0TwoPlayerInitial:	

